from django.contrib import admin
from django.utils.html import format_html
from .models import *

admin.site.site_title = '川塔博客管理后台'  # 修改浏览器标签页显示的标题
admin.site.site_header = '川塔博客管理后台' # 修改登录页面和后台顶部显示的标题

# 站点信息采用单例模式注册
@admin.register(SiteInfo)
class SiteInfoAdmin(admin.ModelAdmin):
    list_display = ('name','bloguser', 'domain', 'is_reward','is_oauth',  'modified_date')
    list_editable = ('domain','bloguser','is_reward', 'is_oauth')

    fieldsets = (
        ('站点信息', {
            'fields': ('name', 'title', 'keywords', 'description', 'domain', 'logo', 'icon', 'background', )
        }),
        ('底部信息', {
            'fields': ('icp', 'public_record','copyright')
        }),
        ('打赏设置', {
            'fields': ('is_reward', 'wechat_reward_qrcode', 'alipay_reward_qrcode', 'is_oauth','oauth_list')
        }),
        ('时间信息', {
            'fields': ('created_date','modified_date')
        })
    )

    def has_add_permission(self, request):
        """限制只能创建单个实例"""
        return not SiteInfo.objects.exists()

# 友情链接带排序功能
@admin.register(FriendLink)
class FriendLinkAdmin(admin.ModelAdmin):
    list_display = ('name', 'url', 'rank','is_lost', 'created_date')
    list_editable = ('rank',)
    list_filter = ('is_lost',)
    search_fields = ('name', )
    ordering = ('-rank', '-created_date')
    readonly_fields = ('created_date', 'modified_date')

# 轮播图带图片预览
@admin.register(Carousel)
class CarouselAdmin(admin.ModelAdmin):
    list_display = ('name', 'image_preview', 'url','is_enable_url', 'is_show', 'rank')
    list_editable = ('rank', 'is_enable_url', 'is_show')
    list_filter = ('is_enable_url', 'is_show')
    ordering = ('-rank', '-created_date')
    readonly_fields = ('created_date', 'modified_date')

    def image_preview(self, obj):
        # 添加空值校验
        if obj.image:
            return format_html(
                '<img src="{}" style="max-height:50px;"/>',
                obj.image.url
            )
        return format_html('<div style="color:gray">暂无图片</div>')  # 空状态提示
    image_preview.short_description = '预览'

# 公告管理
@admin.register(Announcement)
class AnnouncementAdmin(admin.ModelAdmin):
    list_display = ('title', 'is_jump', 'jump_url', 'is_system', 'created_date')
    list_editable = ('is_jump', 'jump_url', 'is_system')
    list_filter = ('is_jump', 'is_system')
    search_fields = ('title',)
    ordering = ('-created_date',)
    readonly_fields = ('created_date', 'modified_date')
# 菜单管理
@admin.register(Menu)
class MenuAdmin(admin.ModelAdmin):
    list_display = ('name', 'url','icon', 'parent', 'is_show', 'rank', 'modified_date')
    list_editable = ('rank','url', 'parent','icon', 'is_show')
    list_filter = ('is_show',)
    search_fields = ('name',)
    ordering = ('-rank', '-created_date')
    readonly_fields = ('created_date', 'modified_date')

    def parent_name(self, obj):
        return obj.parent.name if obj.parent else ""
    parent_name.short_description = '父菜单名称'

    def children(self, obj):
        return obj.menu_set.all()
    children.short_description = '子菜单'

# 第三方登录配置
@admin.register(OauthConfig)
class OauthConfigAdmin(admin.ModelAdmin):
    list_display = ('name', 'app_id', 'app_key','redirect_uri', 'is_enable', 'created_date')
    list_editable = ('app_id', 'app_key', 'is_enable')
    list_filter = ('is_enable',)
    search_fields = ('name',)
    ordering = ('-created_date',)
    readonly_fields = ('created_date', 'modified_date')

# 历史记录


